package com.xgj.exception;

import com.xgj.enums.ErrorCode;

//自定义一次，配合错误枚举类使用
public class DataImportException extends RuntimeException {
    private final ErrorCode errorCode;
    private final String tableName; // 用于存储表名

    // 构造函数，用于不需要表名的情况
    public DataImportException(ErrorCode errorCode, Throwable cause) {
        super(errorCode.getMessage(), cause);
        this.errorCode = errorCode;
        this.tableName = null;
    }

    // 构造函数，用于需要表名的情况
    public DataImportException(ErrorCode errorCode, String tableName, Throwable cause) {
        super(formatMessage(errorCode, tableName), cause);
        this.errorCode = errorCode;
        this.tableName = tableName;
    }

    private static String formatMessage(ErrorCode errorCode, String tableName) {
        String message = errorCode.getMessage();
        if (message.contains("{tableName}")) {
            return message.replace("{tableName}", tableName);
        }
        return message;
    }

    public ErrorCode getErrorCode() {
        return errorCode;
    }

    public String getTableName() {
        return tableName;
    }
}
